package com.atguigu.juc;

import java.util.concurrent.*;

/**
 * @auther zzyy
 * @create 2021-09-03 14:27
 */
public class MyThreadPoolDemo
{
    public static void main(String[] args)
    {


        ExecutorService threadPool = new ThreadPoolExecutor(
                2,
                5,
                2L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardPolicy());
        try
        {
            //模拟20个客户来银行办理业务
            for (int i = 1; i <=10; i++) {
                int finalI = i;
                threadPool.submit(() -> {
                    System.out.println(Thread.currentThread().getName()+"\t 业务员给第："+ finalI +"\t 顾客办理业务");
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            threadPool.shutdown();
        }

    }

    public static void poolInit()
    {
        ExecutorService threadPool = Executors.newFixedThreadPool(5);//一池5线程
        ExecutorService threadPool2 = Executors.newSingleThreadExecutor();//一池一线程
        ExecutorService threadPool3 = Executors.newCachedThreadPool();//一池N线程

        try
        {
            //模拟20个客户来银行办理业务
            for (int i = 1; i <=20; i++) {
                int finalI = i;
                threadPool.submit(() -> {
                    System.out.println(Thread.currentThread().getName()+"\t 业务员给第："+ finalI +"\t 顾客办理业务");
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            threadPool.shutdown();
        }
    }
}
